<?php
// +----------------------------------------------------------------------
// | RXThinkCMF敏捷开发框架 [ 赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2023 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | Licensed LGPL-3.0 RXThinkCMF并不是自由软件，未经许可禁止去掉相关版权
// +----------------------------------------------------------------------
// | 官方网站: https://www.rxthink.cn
// +----------------------------------------------------------------------
// | Author: @牧羊人 团队荣誉出品
// +----------------------------------------------------------------------
// | 版权和免责声明:
// | 本团队对该软件框架产品拥有知识产权（包括但不限于商标权、专利权、著作权、商业秘密等）
// | 均受到相关法律法规的保护，任何个人、组织和单位不得在未经本团队书面授权的情况下对所授权
// | 软件框架产品本身申请相关的知识产权，禁止用于任何违法、侵害他人合法权益等恶意的行为，禁
// | 止用于任何违反我国法律法规的一切项目研发，任何个人、组织和单位用于项目研发而产生的任何
// | 意外、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、
// | 附带或衍生的损失等)，本团队不承担任何法律责任，本软件框架禁止任何单位和个人、组织用于
// | 任何违法、侵害他人合法利益等恶意的行为，如有发现违规、违法的犯罪行为，本团队将无条件配
// | 合公安机关调查取证同时保留一切以法律手段起诉的权利，本软件框架只能用于公司和个人内部的
// | 法律所允许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\service\ClassesService;
use app\admin\service\LoginService;
use app\admin\service\PoliceCenterService;
use app\admin\service\StudentWorkLogService;
use think\Db;

/**
 * 系统登录-控制器
 * @author 牧羊人
 * @since 2020/11/19
 * Class Login
 * @package app\admin\controller
 */
class Login extends Backend
{
    /**
     * 初始化
     * @author 牧羊人
     * @since 2020/11/19
     */
    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->service = new LoginService();
    }

    public function test2()
    {
        $this->service = new StudentWorkLogService();
        $result = $this->service->getList();
        /*$this->service = new ClassesService();
        $result = $this->service->getWorkList();
        return $this->jsonReturn($result);*/
      /*  if(file_exists(date('Y').'year.txt')){
            $res = file_get_contents(date('Y').'year.txt');
        }else{
            $res = file_get_contents('http://timor.tech/api/holiday/year');
            file_put_contents(date('Y').'year.txt',$res);
        }
        $res = json_decode($res,true);*/
        //print_r($res);
        /*    print_r($new_m);
            DIE();*/


       /* $this->service = new PoliceCenterService(18,$this->userId);

        $result = $this->service->getTodayResortTotal();
        return $this->jsonReturn($result);*/

     /*   $t1 = strtotime(date('Y-m-d', strtotime("this week")));
        $t2 = strtotime(date('Y-m-d 23:59', strtotime("this week +6 days")));
        $where2 = [];
        $where2[] = ['belong_time',['>',$t1],['<',$t2]];
        $where3 = [
            'schoole_id' => 20,
            'grade_id' => 1,
        ];
        $a = Db::table('think_grade_attendance')->where($where3)->where($where2)->sum('attendance');

        $c = Db::table('think_grade_attendance')->where($where3)->where($where2)->count();*/
    }


    //统计出勤率
    public function attend()
    {
        //$d = strtotime(date('Y-m-d 00:00:00')) - 1;
        $d = date('Y-m-d');
        //echo $d;
        daykaoqin($d);
       /* $list = Db::table('think_student_work_log')->where('day != 20241104')->group('day')->select();
        foreach($list as $k=>$v){
            $v['day'] = date('Y-m-d',strtotime($v['day']));
            daykaoqin($v['day']);
        }*/

    }

    public function classupgrade()
    {
        if(date('m-d') == '03-01') {
            $class = Db::table('think_class')->where('mark', 1)
                ->order('class_key desc')
                ->select();
            foreach ($class as $v) {
                $map1 = [
                    'schoole_id' => $v['schoole_id'],
                    "mark" => 1,
                ];
                $g = Db::table('think_grade')
                    ->where($map1)
                    ->where("grade_key > {$v['grade_id']}")
                    ->order('grade_key asc')
                    ->find();
                //升级年级
                if ($g) {
                    $up = [
                        "grade_id" => $g["grade_key"],
                    ];
                    Db::table('think_class')->where('id', $v['id'])->update($up);
                    $map = [
                        'schoole_id' => $v['schoole_id'],
                        'grade_id' => $v['grade_id'],
                        'class_id' => $v['class_key'],
                        'mark' => 1,
                        'up_lock' => 0,
                    ];
                    //学生年级升级
                    $list = Db::table('think_student')->where($map)->field('id')->select();
                    //update([ "grade_id" => $g["grade_key"]]);
                    foreach ($list as $v1) {
                        Db::table('think_student')->where('id', $v1['id'])->update(["grade_id" => $g["grade_key"], 'up_lock' => 1]);
                    }
                } else {
                    //视为毕业班级，移除老师学生年级关系
                    //$c = Db::table('think_class')->where('id', $v['id'])->find();
                    //$t = $c['teacher_ids'];
                   /* //带多个班级
                    if (strpos($t, ',') !== false) {
                        $explode = explode(',', $t);
                        $index = 1;
                        array_splice($explode, $index, 1);
                        $ids = implode(',', $explode);
                        Db::table('think_class')->where('id', $v['id'])->update(['teacher_ids' => $ids]);
                    } else {*/
                        //只带了一个班级
                    Db::table('think_class')->where('id', $v['id'])->update(['mark' => 0]);
                    //}
                    $map = [
                        'schoole_id' => $v['schoole_id'],
                        'grade_id' => $v['grade_id'],
                        'class_id' => $v['class_key'],
                        'mark' => 1,
                        'up_lock' => 0,
                    ];
                    $list = Db::table('think_student')->where($map)->field('id')->select();
                    foreach ($list as $v1) {
                        Db::table('think_student')->where('id', $v1['id'])->update(["mark" => 0, 'del_remark' => '升级删除']);
                    }
                }
            }
            Db::table('think_student')->where('up_lock', 1)->update(['up_lock' => 0]);
        }
    }

    public function test()
    {
    }

    /**
     * 验证码
     * @return mixed
     * @since 2020/11/14
     * @author 牧羊人
     */
    public function captcha()
    {
        $result = $this->service->captcha();
        return $this->jsonReturn($result);
    }

    /**
     * 登录
     * @return mixed
     * @since 2020/11/14
     * @author 牧羊人
     */
    public function login()
    {
        $result = $this->service->login();
        return $this->jsonReturn($result);
    }

    /**
     * 退出系统
     * @return mixed
     * @since 2020/11/14
     * @author 牧羊人
     */
    public function logout()
    {
        $result = $this->service->logout();
        return $this->jsonReturn($result);
    }

    public function appLogin()
    {
        $result = $this->service->appLogin();
        return $this->jsonReturn($result);
    }

    /**
     * 退出系统
     * @return mixed
     * @since 2020/11/14
     * @author 牧羊人
     */
    public function appLogout()
    {
        $result = $this->service->appLogout();
        return $this->jsonReturn($result);
    }

    public function miniAppLogin()
    {
        $result = $this->service->miniAppLogin();
        return $this->jsonReturn($result);
    }

    public function miniAppLogout()
    {
        $result = $this->service->miniAppLogout();
        return $this->jsonReturn($result);
    }
}